﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using woaC2.LogicLib.DataContract.Output;
using woaC2.LogicLib.DataContract.Input;
using woaPoker.LogicLib.Database;
using Newtonsoft.Json;
using woaC2.Common;
using woaC2.SocketLib;

namespace woaPoker.LogicLib.LogicCtrl
{
    class WaitRoomGameLgCtrl
    {
        #region ducon
       
        //remove nguoi choi khoi tran dau 
        public Flag GameCancel(Guid sessionId,MatchInDc match)
        {
            var b = new Flag();
            if (match.sessionIdMatch == sessionId)
            {
                MathRegistry.instance.Remove(sessionId);
                b.fg = true;
            }
            else
            {
                MathRegistry.instance.GetMatchById(match.sessionIdMatch).listPlayer1.Remove(PlayerRegistry.instance.GetPlayer(sessionId));
                MathRegistry.instance.GetMatchById(match.sessionIdMatch).listPlayer2.Remove(PlayerRegistry.instance.GetPlayer(sessionId));
                b.fg = false;
            }
            var jsonOut = "mes001$"+JsonConvert.SerializeObject(b);
            var mesOut = FunctionCmn.StampMessage(jsonOut);
            SocketServer.Instance.BroadcastToClientInMatch(MathRegistry.instance.GetMatchById(match.sessionIdMatch),mesOut);
            return b;
        }
        public Flag RemovePlayer(Guid sessionId, PlayerInDC player)
        {
            var b = new Flag();
            //Lay ra tran dau nguoi choi tham gia
            var match = MathRegistry.instance.GetMatchById(sessionId);
            if (match!=null && sessionId!=player.sessionId)
            {
                match.listPlayer1.Remove(PlayerRegistry.instance.GetPlayer(player.sessionId));
                match.listPlayer2.Remove(PlayerRegistry.instance.GetPlayer(player.sessionId));
                b.fg = true;
            }
            else
            {
                b.fg = false;
                return b;
            }
            var jsonOut = "mes103$" + JsonConvert.SerializeObject(b);
            var mesOut = FunctionCmn.StampMessage(jsonOut);
            SocketServer.Instance.BroadcastToClientInMatch(match, mesOut);
            //send something for kicked play
            //SocketServer.Instance.SendToClient(player.sessionId, mesOut);
            return b;
        }
        public Flag ChangeTeam(Guid sessionId, MatchInDc match)
        {
            var b = new Flag();
            //Lay ra tran dau nguoi choi tham gia
            var obj = MathRegistry.instance.GetMatchById(match.sessionIdMatch);
            if (obj.listPlayer1.Where(s => s.client.SessionID == sessionId).FirstOrDefault() != null)
            {
                if (obj.listPlayer2.Count() < 3)
                {
                    obj.listPlayer1.Remove(PlayerRegistry.instance.GetPlayer(sessionId));
                    obj.listPlayer2.Add(PlayerRegistry.instance.GetPlayer(sessionId));
                    b.fg = true;
                }
                else
                {
                    b.fg = false;
                }
            }
            else
            {
                if (obj.listPlayer1.Count() < 3)
                {
                    obj.listPlayer2.Remove(PlayerRegistry.instance.GetPlayer(sessionId));
                    obj.listPlayer1.Add(PlayerRegistry.instance.GetPlayer(sessionId));
                    b.fg = true;
                }
                else
                {
                    b.fg = false;
                }
            }
            var jsonOut = "mes103$";
            var mesOut = FunctionCmn.StampMessage(jsonOut);
            SocketServer.Instance.BroadcastToClientInMatch(MathRegistry.instance.GetMatchById(match.sessionIdMatch), mesOut);
            return b;
        }
        public List<InfPlayerDC> ListPlayer(Guid sessionId,MatchInDc match)
        {
            List<InfPlayerDC> li = new List<InfPlayerDC>();
            //Lay ra tran dau nguoi choi tham gia
            var obj = MathRegistry.instance.GetMatchById(match.sessionIdMatch);
            for (int i = 0; i < obj.listPlayer1.Count; i++) // Loop through List with for
            {
                InfPlayerDC pl = new InfPlayerDC();
                pl.type = 1;
                pl.sessionId = obj.listPlayer1[i].client.SessionID;
                if (pl.sessionId == obj.sessionIdMatch)
                {
                    pl.isBoss = true;
                }
                else pl.isBoss = false;
                //TankDataContext db = new TankDataContext();
                pl.userName = PlayerRegistry.instance.GetPlayer(pl.sessionId).userName; //db.Users.Where(s => s.UserID == PlayerRegistry.instance.GetPlayer(sessionId).UserID).FirstOrDefault().Username;
                li.Add(pl);
            }
            for (int i = 0; i < obj.listPlayer2.Count; i++) // Loop through List with for
            {
                InfPlayerDC pl = new InfPlayerDC();
                pl.type = 2;
                pl.sessionId = obj.listPlayer2[i].client.SessionID;
                if (pl.sessionId == obj.sessionIdMatch)
                {
                    pl.isBoss = true;
                }
                else pl.isBoss = false;
                //TankDataContext db = new TankDataContext();
                pl.userName = PlayerRegistry.instance.GetPlayer(pl.sessionId).userName;//db.Users.Where(s => s.UserID == PlayerRegistry.instance.GetPlayer(sessionId).UserID).FirstOrDefault().Username;
                li.Add(pl);
            }
            return li;
        }

        //public List<InfPlayerDC> ListPlayer(Guid sessionId)
        //{
        //    List<InfPlayerDC> li = new List<InfPlayerDC>();
        //    //Lay ra tran dau nguoi choi tham gia
        //    //var obj = MathRegistry.instance.GetMatchById(match.sessionId);
        //    for (int i = 0; i < 2; i++) // Loop through List with for
        //    {
        //        InfPlayerDC pl = new InfPlayerDC();
        //        pl.type = 1;
        //        pl.sessionId = Guid.NewGuid();
              
        //            pl.isBoss = true;
                

        //        pl.userName = "anhday"; //db.Users.Where(s => s.UserID == PlayerRegistry.instance.GetPlayer(sessionId).UserID).FirstOrDefault().Username;
        //        li.Add(pl);
        //    }
        //    for (int i = 0; i < 2; i++) // Loop through List with for
        //    {
        //        InfPlayerDC pl = new InfPlayerDC();
        //        pl.type = 2;
        //        pl.sessionId = Guid.NewGuid();

        //            pl.isBoss = true;



        //        pl.userName = "EM DAY";//db.Users.Where(s => s.UserID == PlayerRegistry.instance.GetPlayer(sessionId).UserID).FirstOrDefault().Username;
        //        li.Add(pl);
        //    }
        //    return li;
        //}
        #endregion 
    }
}
